package cn.gdcp.tabdemo;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    // 底部菜单4个Linearlayout
     private LinearLayout ll_home;
     private LinearLayout ll_friend;

              // 底部菜单4个ImageView
             private ImageView iv_home;
      private ImageView iv_friend;

              // 底部菜单4个菜单标题
             private TextView tv_home;
     private TextView tv_friend;
              // 4个Fragment
             private Fragment homeFragment;
      private Fragment friendFragment;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化控件
              initView();
                // 初始化底部按钮事件
                initEvent();
               // 初始化并设置当前Fragment
                initFragment(0);
    }

    private void initFragment(int i) {
        // 由于是引用了V4包下的Fragment，所以这里的管理器要用getSupportFragmentManager获取
             FragmentManager fragmentManager = getSupportFragmentManager();
          // 开启事务
                FragmentTransaction transaction = fragmentManager.beginTransaction();
             // 隐藏所有Fragment
              hideFragment(transaction);
           switch (i) {
                  case 0:
                           if (homeFragment == null) {
                               homeFragment = new HomeFragment();
                                    transaction.add(R.id.fl_content, homeFragment);
                              } else {
                                 transaction.show(homeFragment);
                              }
                          break;
                  case 2:
                           if (friendFragment == null) {
                                     friendFragment = new FriendFragment();
                                 transaction.add(R.id.fl_content, friendFragment);
                             } else {
                                   transaction.show(friendFragment);
                              }

                            break;

                    default:
                           break;
                   }

            // 提交事务*/
             transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (homeFragment != null) {
                       transaction.hide(homeFragment); }

              if (friendFragment != null) {
                      transaction.hide(friendFragment);
                    }


    }

    private void initView() {
        // 底部菜单4个Linearlayout
             this.ll_home = (LinearLayout) findViewById(R.id.ll_home);
             this.ll_friend = (LinearLayout) findViewById(R.id.ll_friend);


                 // 底部菜单4个ImageView
               this.iv_home = (ImageView) findViewById(R.id.iv_home);
                this.iv_friend = (ImageView) findViewById(R.id.iv_friend);

                // 底部菜单4个菜单标题
             this.tv_home = (TextView) findViewById(R.id.tv_home);
               this.tv_friend = (TextView) findViewById(R.id.tv_friend);

    }
    private void initEvent(){
        // 设置按钮监听
           ll_home.setOnClickListener(this);
              ll_friend.setOnClickListener(this);

    }

    public void onClick(View v) {
        // 在每次点击后将所有的底部按钮(ImageView,TextView)颜色改为灰色，然后根据点击着色
              restartBotton();
             // ImageView和TetxView置为绿色，页面随之跳转
                switch (v.getId()) {
                    case R.id.ll_home:
                      //   iv_home.setImageResource(R.drawable.tab_weixin_pressed);
                           tv_home.setTextColor(0xff1B940A);
                         initFragment(0);
                          break;

                 case R.id.ll_friend:
                      //   iv_friend.setImageResource(R.drawable.tab_find_frd_pressed);
                            tv_friend.setTextColor(0xff1B940A);
                           initFragment(2);
                          break;

                    default:
                            break;
                  }

    }

    private void restartBotton() {
        // ImageView置为灰色
          //   iv_home.setImageResource(R.drawable.tab_weixin_normal);
         //      iv_friend.setImageResource(R.drawable.tab_find_frd_normal);
                // TextView置为白色
              tv_home.setTextColor(0xffffffff);
               tv_friend.setTextColor(0xffffffff);



    }
}
